package com.chengxiaoxiao.dao.impl;

import com.chengxiaoxiao.dao.BlackListDao;
import com.chengxiaoxiao.dao.ClassesDao;
import com.chengxiaoxiao.dao.UsersDao;
import com.chengxiaoxiao.domain.BlackList;
import com.chengxiaoxiao.domain.Classes;
import com.chengxiaoxiao.domain.Users;
import com.chengxiaoxiao.domain.UsersExtra;
import com.chengxiaoxiao.utils.db.*;

import java.util.ArrayList;
import java.util.List;

public class BlackListDaoImpl implements BlackListDao {
    private DBAssist da = new DBAssist(DBCPUtil.getDataSource());
    private ClassesDao classesDao = new ClassesDaoImpl();
    private UsersDao usersDao = new UsersDaoImpl();

    @Override
    public void add(BlackList blackList) {

        da.update("insert into blacklist(cause,subTime,users_id) values(?,?,?)", blackList.getCause(), blackList.getSubTime(), blackList.getUsers_Id());

    }

    @Override
    public void update(BlackList blackList) {
        da.update("update blacklist set cause=?,subTime=?,users_id=? where id=?",
                blackList.getCause(), blackList.getSubTime(), blackList.getUsers_Id(), blackList.getId());

    }

    @Override
    public void delete(long id) {

        BlackList list = getbyId(id);
        int uid = (int) list.getUsers_Id();

        Users user = usersDao.getbyId(uid);
        user.setIsBlack(0);
        usersDao.update(user);

        da.update("delete from blacklist where id=?", id);
    }

    @Override
    public List get() {
        return (List) da.query("select * from blacklist", new BeanListHandler(BlackList.class));
    }

    @Override
    public BlackList getbyId(long id) {
        BlackList blackList = (BlackList) da.query("select * from blacklist where id=?", new BeanHandler(BlackList.class), id);
        if (blackList != null) {
            Users user = (Users) da.query("select * from users where id=?", new BeanHandler(Users.class), blackList.getUsers_Id());
            if (user != null) {
                Classes cla = classesDao.getbyId(user.getId());
                if (cla.getCollegeId() != 0) {
                    Classes c = classesDao.getbyId(cla.getCollegeId());
                    cla.setClasses(c);
                }
                user.setClasses(cla);
                UsersExtra extra = (UsersExtra) da.query("select * from usersextra where userId=?", new BeanHandler(UsersExtra.class), user.getId());
                user.setUsersExtra(extra);
            }
            blackList.setUser(user);
        }
        return blackList;
    }

    @Override
    public List getListPage(int startIndex, int pageSize) {

        String sql = "select * from blacklist limit ?,?";
        List<BlackList> list = (List) da.query(sql, new BeanListHandler(BlackList.class), startIndex, pageSize);

        List<BlackList> model = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            BlackList blackList = list.get(i);
            if (blackList != null) {
                Users user = (Users) da.query("select * from users where id=?", new BeanHandler(Users.class), blackList.getUsers_Id());


                if (user != null) {
                    Classes cla = classesDao.getbyId(user.getClassesId());
                    if (cla.getCollegeId() != 0) {
                        Classes c = classesDao.getbyId(cla.getCollegeId());
                        cla.setClasses(c);
                    }
                    user.setClasses(cla);
                    UsersExtra extra = (UsersExtra) da.query("select * from usersextra where userId=?", new BeanHandler(UsersExtra.class), user.getId());
                    user.setUsersExtra(extra);
                }
                blackList.setUser(user);
            }
            model.add(blackList);
        }

        return model;

    }

    @Override
    public int getTotalNums() {

        return (int) da.query("select COUNT(*) from blacklist", new ScalarHandler());
    }

    @Override
    public List getPageLikeSNum(int startIndex, int pageSize, String snum) {
        String sql = "select * from blacklist limit ?,?";
        List<BlackList> list = (List) da.query(sql, new BeanListHandler(BlackList.class), startIndex, pageSize);

        List<BlackList> model = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            BlackList blackList = list.get(i);
            if (blackList != null) {
                Users user = (Users) da.query("select * from users where id=? and studentNum like?", new BeanHandler(Users.class), blackList.getUsers_Id(), "%" + snum + "%");

                if (user != null) {
                    Classes cla = classesDao.getbyId(user.getClassesId());
                    if (cla.getCollegeId() != 0) {
                        Classes c = classesDao.getbyId(cla.getCollegeId());
                        cla.setClasses(c);
                    }
                    user.setClasses(cla);
                    UsersExtra extra = (UsersExtra) da.query("select * from usersextra where userId=?", new BeanHandler(UsersExtra.class), user.getId());
                    user.setUsersExtra(extra);
                    blackList.setUser(user);
                    model.add(blackList);
                }

            }

        }

        return model;
    }
}
